package com.martin.fastframe.ui.like.follow

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.LinearInterpolator
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.animation.addListener
import com.martin.fastframe.R
import com.martin.lib_base.binding.adapter.loadImage
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.ktx.tryCatch
import dev.utils.app.HandlerUtils
import dev.utils.app.anim.AnimationUtils

object LikeFollowModel {

    //短视频展示数据
    val videoLikeFollowData: ILikeFollowData by lazy {
        VideoLikeFollowData()
    }

    //直播展示数据
    val liveLikeFollowData: ILikeFollowData by lazy {
        LiveLikeFollowData()
    }

    /**
     * 展示点赞效果
     * @param x: 点击的x坐标
     * @param y: 点击的y坐标
     * @param viewGroup: 父布局
     * @param data: 展示数据
     */
    fun show(x: Int, y: Int, viewGroup: ViewGroup, data: ILikeFollowData = videoLikeFollowData) {
        //添加爱心
        val likeView = AppCompatImageView(viewGroup.context)
        //加载图片
        loadImage(likeView, data.getIconList().random())

        val size = data.getIconSize()
        val layoutParams = ViewGroup.LayoutParams(size, size)
        likeView.layoutParams = layoutParams
        //设置爱心位置
        likeView.x = (x - (size / 2)).toFloat()
        likeView.y = (y - size + data.getYOffset()).toFloat()
        //设置随机旋转角度
        likeView.rotation = (data.getRotationRange().random()).toFloat()
        viewGroup.addView(likeView)

        //获取动画集合
        val animatorSet = data.getAnimatorSet(likeView)
        //添加动画结束监听
        animatorSet.addListener(onEnd = {
            tryCatch({
                //清理动画
                likeView.clearAnimation()
                //隐藏图片
                likeView.visibility = View.GONE
                //移除图片,延迟500毫秒移除,防止动画还没结束就移除
                HandlerUtils.postRunnable({
                    viewGroup.removeView(likeView)
                }, 500)
            })
        })
        animatorSet.start()
    }
}